<?php

namespace App\Http\Admin\Order\Controllers;

//调用系统类
use Framework\BaseClass\Http\Admin\Controller;
use Illuminate\Http\Request;

//调用自定义类
use App\Http\Admin\Order\Models\Order;
use App\Http\Admin\OrderType\Models\OrderType;
use App\Http\Admin\Order\Models\OrderProcess;

use App\Eloquent\Ygt\OrderPre;
use App\Eloquent\Ygt\Order as OrderEloquent;
use App\Eloquent\Ygt\DepartmentUser;
use App\Eloquent\Ygt\OrderProcess as OrderProcessEloquent;
use App\Eloquent\Ygt\Permission;
use App\Eloquent\Ygt\Message;
use App\Eloquent\Ygt\Process;
use App\Eloquent\Ygt\OrderProcessPre;
use App\Eloquent\Ygt\OrderDistributionPre;
use App\Eloquent\Ygt\OrderDistribution;
use App\Eloquent\Ygt\OrderMaterial;
use App\Eloquent\Ygt\ImgUpload;
use App\Eloquent\Ygt\ProcessField;
use App\Eloquent\Ygt\OrderReportType as OrderReportTypeEloquent;
use App\Eloquent\Ygt\OrderReportCell as OrderReportCellEloquent;
use App\Eloquent\Ygt\Privilege;

use App\Engine\OrderType as OrderTypeEngine;
use App\Engine\Permission as PermissionEngine;
use App\Engine\Func;
use App\Engine\Product as ProductEngine;
use App\Engine\OrderEngine;

use App\Eloquent\Province;
use App\Eloquent\City;
use App\Eloquent\Area;


/**
 * Created by PhpStorm.
 * User: zhuyujun
 * Date: 2017/11/9
 * Time: 14:46
 */
class IndexController extends Controller
{
    //获取工单列表基础页面
    public function getOrderList()
    {
        return $this->view('order-list');
    }

    //获取工单列表数据
    public function getOrderListData(Request $request)
    {
        //后台获取用户对应的企业id
        $adminUser = request()->user('admin')->toArray();
        $companyId = $adminUser['company_id'];

        $input = $request->all();
        $callBack = $input['callback'];
        $searchOrderTitle = isset($input['search_order_title']) ? $input['search_order_title'] : '';


        $order = new Order();
        $where = [];
        $where[] = ['company_id','=', $companyId];
        if($searchOrderTitle){
            $where[] = ['order_title', 'like', '%' . $searchOrderTitle . '%'];
        }
        $orderList = $order->getOrderList($where);

        //数据调整
        foreach ($orderList as $tmpOrderRow) {
            $tmpOrderRow->order_type = $order->getOrderOrderTypeById($tmpOrderRow->order_type);//工单类型
            $tmpOrderRow->order_status = $order->getOrderStatusById($tmpOrderRow->status);//工单状态
            $tmpOrderRow->create_user = $order->getUserNameById($tmpOrderRow->uid);//下单人
            $tmpOrderRow->create_time = $tmpOrderRow->created_at->formatLocalized('%Y%m%d %H:%M:%S');//创建时间
        }

        echo $callBack . '(' . $orderList->toJson() . ')';
        die();
    }

    //工单详情
    public function getOrderDetail(Request $request)
    {
        $input = $request->all();

        $order = new Order();
        $showList = $order->getOrderDetail($input);

        //获取工单详情数据

        return $this->view('order-detail', compact('showList'));
    }

    //工单工序列表基础页面
    public function getOrderProcessList(Request $request)
    {
        $input = $request->all();
        $orderId = isset($input['order_id']) ? $input['order_id'] : '';

        return $this->view('order-process-list', compact('orderId'));
    }

    //工单工序列表数据
    public function getOrderProcessListData(Request $request)
    {
        //后台获取用户对应的企业id
        $adminUser = request()->user('admin')->toArray();
        $companyId = $adminUser['company_id'];

        $input = $request->all();
        $callBack = $input['callback'];
        $orderId = isset($input['order_id']) ? $input['order_id'] : '';
        $searchStatus = isset($input['search_status']) ? $input['search_status'] : '';

        $order = new Order();
        $where = [];
        $where = ['company_id' => $companyId];
        if ($orderId) {
            $where['order_id'] = $orderId;
        }

        if($searchStatus){
            $where['status'] =$searchStatus;
        }

        $orderProcessList = $order->getOrderProcessList($where);

        //数据调整
        foreach ($orderProcessList as $tmpOrderProcessRow) {
            $tmpOrderProcessRow->process_title = $order->getProcessTitle($tmpOrderProcessRow->process_type);//工序名称

            //开工时间
            if ($tmpOrderProcessRow->start_time) {
                $tmpOrderProcessRow->start_time = date('Ymd H:i:s', $tmpOrderProcessRow->start_time);
            } else {
                $tmpOrderProcessRow->start_time = '';
            }


            //预计完成时间
            if ($tmpOrderProcessRow->status != 1) {
                if (!(int)$tmpOrderProcessRow->estimated_time_hour) $tmpOrderProcessRow->estimated_time_hour = 0;
                if (!(int)$tmpOrderProcessRow->estimated_time_minute) $tmpOrderProcessRow->estimated_time_minute = 0;
                $tmpOrderProcessRow->estimated_time = date('Ymd H:i:s', (strtotime($tmpOrderProcessRow->start_time) + $tmpOrderProcessRow->estimated_time_hour * 3600 + $tmpOrderProcessRow->estimated_time_minute * 60));
                $tmpOrderProcessRow->estimated_time .= "({$tmpOrderProcessRow->estimated_time_hour}h{$tmpOrderProcessRow->estimated_time_minute}m)";
            } else {
                $tmpOrderProcessRow->estimated_time = '';
            }

            //确认完工时间
            if ($tmpOrderProcessRow->status == 4) {
                $tmpOrderProcessRow->completed_time = $tmpOrderProcessRow->updated_at->formatLocalized('%Y%m%d %H:%M:%S');//开工时间
            } else {
                $tmpOrderProcessRow->completed_time = '';
            }

            $tmpOrderProcessRow->process_order_status = $order->getOrderProcessStatusById($tmpOrderProcessRow->status);//工单状态
            $tmpOrderProcessRow->accept_user = (string)$order->getUserNameById($tmpOrderProcessRow->uid);//接单人
        }

        echo $callBack . '(' . $orderProcessList->toJson() . ')';
        die();
    }

    //工单工序详情
    public function getOrderProcessDetail(Request $request)
    {
        $input = $request->all();

        $order = new Order();
        $showList = $order->getOrderProcessDetail($input);

        //获取工单详情数据

        return $this->view('order-process-detail', compact('showList'));
    }

    //创建工单-工单类型选择列表
    public function createOrder()
    {
        //用户id&企业id
        $userId = request()->header('userid', 0);
        $adminUser = request()->user('admin')->toArray();
        $userId = $adminUser['id'];
        $companyId = $adminUser['company_id'];

        if (request()->isMethod('post')) {
            $where = [];

            $pageSize = request('pageSize', 30);
            $pageCurrent = request('pageCurrent', 1);
            $offset = ($pageCurrent - 1) * $pageSize;

            $orderTypeModel = new orderType();
            $fields = '*';
            $where = ['company_id' => $companyId];
            $orderTypeResult = $orderTypeModel->getData($where, $fields, $pageSize, $offset);

            $orderTypeObj = new OrderTypeEngine();
            $where = ['company_id' => $companyId];
            $orderTypeResult = $orderTypeObj->getOrderTypeList($where, $pageSize, $offset);

            $orderTypeResult->transform(function ($item, $key) {
                $item->img_url = Func::getImgUrlById($item->img_id);
                return $item;
            });

            $count = $orderTypeObj->getCount($where);
            $result = collect(['totalRow' => $count, 'pageCurrent' => $pageCurrent, 'list' => $orderTypeResult]);

            return $result->toJson();
        }

        return $this->view('create-order');
    }

    //创建工单-工单类型选择列表
    public function orderTypeSubmit(Request $request)
    {
        //获取传入数据
        $input = $request->all();
        //获取工单类型id
        //校验参数
        $rules = [
            'order_type' => 'required',
        ];
        $message = [
            'order_type.required' => '未选择工单类型'
        ];
        $this->validateRequestParameters($input, $rules, true, $message);

        //创建预生成工单&返回预生成工单id
        $orderPreRow = OrderPre::firstOrNew(['id' => '']);

        //生成工单号
        $orderTitle = 'D' . time() . rand(0, 9) . rand(0, 9) . rand(0, 9) . rand(0, 9) . rand(0, 9) . rand(0, 9);

        //用户id&企业id
        $userId = request()->header('userid', 0);
        $adminUser = request()->user('admin')->toArray();
        $userId = $adminUser['id'];
        $companyId = $adminUser['company_id'];


        $insertRow = [
            'order_type' => $input['order_type'],
            'order_title' => $orderTitle,
            'uid' => $userId,
            'company_id' => $companyId,
            'status' => 4,//工单状态 4-待派发
            'is_trash' => 1,//默认设置为该记录为可删除
        ];

        $orderPreRow->fill($insertRow);
        $orderPreRow->save();
        $orderPreId = $orderPreRow->id;

        $re = header("location:/admin/order/createOrderDetail?order_pre_id={$orderPreId}");
        var_dump($re);
        die();
    }

    //创建工单详情页
    public function createOrderDetail(Request $request)
    {
        //获取传入数据
        $input = $request->all();
        $input['copy_order_id'] = isset($input['copy_order_id']) ? $input['copy_order_id'] : '';//被复制的工单id 测试数据

        //获取工单类型id
        //校验参数
        $rules = [
            'order_pre_id' => 'required',
        ];
        $message = [
            'order_pre_id.required' => '参数错误'
        ];
        $this->validateRequestParameters($input, $rules, true, $message);

        $curOrderRow = [];
        if ($input['copy_order_id']) {//如果传入复制工单id
            $curOrderRow = OrderEloquent::where(['id' => $input['copy_order_id']])->first()->toArray();
        }


        //获取工单基础信息
        $orderPreId = $input['order_pre_id'];
        $orderModel = new Order();
        $where = ['id' => $orderPreId];
        $orderPreRow = $orderModel->getOrderPreInfo($where);

        /****工单创建详情页-基础信息****/
        //工单基础信息字段，目前格式固定
        //用户id&企业id
        $userId = request()->header('userid', 0);
        $adminUser = request()->user('admin')->toArray();
        $userId = $adminUser['id'];
        $companyId = $adminUser['company_id'];
        $orderEngine = new OrderEngine();
        $orderBaseFieldList = $orderEngine->orderBaseFieldList($companyId);

        //添加默认值
        if (!empty($curOrderRow)) {
            foreach ($orderBaseFieldList as $key => $row) {
                if (isset($curOrderRow[$row['field_name']]) && $curOrderRow[$row['field_name']]) {
                    if ($row['field_type'] == 5) {//填写+单位选择 处理
                        $oldRow = $curOrderRow[$row['field_name']];
                        $oldRowArr = explode(',', $oldRow);
                        $defaultValue = $oldRowArr[0];
                        $oldUnitTitle = $oldRowArr[1];
                        $oldUnitId = $this->getUnitIdByTitle($oldUnitTitle, $row['field_unit']);
                        $orderBaseFieldList[$key]['default_value'] = $defaultValue;
                        $orderBaseFieldList[$key]['default_unit_id'] = $oldUnitId;
                        $orderBaseFieldList[$key]['default_unit_title'] = $oldUnitTitle;
                    } elseif ($row['field_type'] == 9) {//图片处理
                        $oldImgRow = $curOrderRow[$row['field_name']];
                        $imgIdList = explode(',', $oldImgRow);

                        $imgUrlListStr = '';
                        foreach ($imgIdList as $imgId) {
                            if (!trim($imgId)) {//过滤空格 图片id应该不会有0
                                continue;
                            }
                            $imgUrl = ImgUpload::getImgUrlById($imgId);
                            $imgUrlListStr .= $imgUrl . ',';
                        }

                        $imgUrlListStr = trim($imgUrlListStr, ',');
                        $orderBaseFieldList[$key]['default_img_id'] = $oldImgRow;
                        $orderBaseFieldList[$key]['default_img_url'] = $imgUrlListStr;

                    } else {
                        $orderBaseFieldList[$key]['default_value'] = $curOrderRow[$row['field_name']];
                    }
                } elseif ($row['field_type'] == 8) {//配送地址 处理
                    //获取之前的工单id的配送信息
                    $oldOrderDistribution = OrderDistribution::where(['order_id' => $input['copy_order_id']])->get()->toArray();
                    $oldOrderDistributionRow = !empty($oldOrderDistribution[0]) ? $oldOrderDistribution[0] : [];
                    if (!empty($oldOrderDistributionRow)) {
                        $defaultDistributionRow = [];
                        $defaultDistributionRow['consignee'] = $oldOrderDistributionRow['consignee'];//收货人
                        $defaultDistributionRow['phone'] = $oldOrderDistributionRow['phone'];//电话
                        $defaultDistributionRow['province_id'] = $oldOrderDistributionRow['province_id'];//省-id
                        $defaultDistributionRow['province_name'] = $oldOrderDistributionRow['province_name'];//省-名称
                        $defaultDistributionRow['city_id'] = $oldOrderDistributionRow['city_id'];//市-id
                        $defaultDistributionRow['city_name'] = $oldOrderDistributionRow['city_name'];//市-名称
                        $defaultDistributionRow['area_id'] = $oldOrderDistributionRow['area_id'];//区-id
                        $defaultDistributionRow['area_name'] = $oldOrderDistributionRow['area_name'];//区-名称
                        $defaultDistributionRow['address'] = $oldOrderDistributionRow['address'];//详细地址
                        $orderBaseFieldList[$key]['default_distribution_row'] = $defaultDistributionRow;
                    }
                }
            }
        }

        /****工单创建详情页-工单信息****/
        $processList = [];
        //第一个工序
        $orderTypeObj = new OrderTypeEngine();
        $firstProcessId = $orderTypeObj->getFirstOneProcessId($orderPreRow['order_type']);
        $curProcessId = $firstProcessId;

        while ($curProcessId) {
            //通过工序id，获取对应的字段
            $processFieldList = Process::getFieldListByProcessId($curProcessId);
            //获取工序信息
            $processTitle = Process::getOneValueById($curProcessId, 'title');

            $curOrderProcessRow = [];
            if ($input['copy_order_id']) {//如果传入复制工单id
                $curOrderProcessRow = OrderProcessEloquent::where(['order_id' => $input['copy_order_id'], 'process_type' => $curProcessId])->first();
                if (!empty($curOrderProcessRow)) {
                    $curOrderProcessRow = $curOrderProcessRow->toArray();
                }
            }

            //添加默认值
            if (!empty($curOrderProcessRow)) {
                foreach ($processFieldList as $key => $row) {
                    if (isset($curOrderProcessRow[$row['field_name']])) {
                        if ($row['field_type'] == 3 && $curOrderProcessRow[$row['field_name']]) {//选择 处理
                            $selectId = $this->getSidByTitle($curOrderProcessRow[$row['field_name']], $row['data']);
                            $processFieldList[$key]['default_select_id'] = $selectId;
                            $processFieldList[$key]['default_value'] = $curOrderProcessRow[$row['field_name']];
                        } elseif ($row['field_type'] == 4 && $curOrderProcessRow[$row['field_name']]) {//选择材料页面
                            $materialIdListStr = $curOrderProcessRow[$row['field_name']];
                            $materialIdList = explode(',', $materialIdListStr);

                            $materialList = [];
                            foreach ($materialIdList as $materialId) {
                                if (!$materialId) {//过滤异常数据
                                    continue;
                                }

                                $materiaRow = ProductEngine::getProductInfo($materialId)->toArray();
                                $materialList[] = [
                                    'id' => $materialId,
                                    'product_no' => $materiaRow['product_no'],
                                    'product_name' => $materiaRow['product_name'],
                                ];
                            }
                            $processFieldList[$key]['default_product_list'] = $materialList;
                        } elseif ($row['field_type'] == 5) {//填写+单位选择 处理
                            $oldRow = $curOrderProcessRow[$row['field_name']];
                            $oldRowArr = explode(',', $oldRow);
                            $defaultValue = $oldRowArr[0];
                            $oldUnitTitle = $oldRowArr[1];
                            $oldUnitId = $this->getUnitIdByTitle($oldUnitTitle, $row['field_unit']);
                            $processFieldList[$key]['default_value'] = $defaultValue;
                            $processFieldList[$key]['default_unit_id'] = $oldUnitId;
                            $processFieldList[$key]['default_unit_title'] = $oldUnitTitle;
                        } elseif ($row['field_type'] == 11) {//双联动
                            $oldRow = $curOrderProcessRow[$row['field_name']];
                            $tmpArr = explode(',', $oldRow);
                            //双联动第一级默认选择
                            if (isset($tmpArr[0])) {
                                foreach ($processFieldList[$key]['data'] as $tmpKey => $tmpData) {
                                    if ($tmpData['title'] == $tmpArr[0]) {
                                        $processFieldList[$key]['data'][$tmpKey]['isSelect'] = 1;
                                    }
                                    //双联动第二级默认选择
                                    if (isset($tmpArr[1])) {
                                        $selectChild = [];
                                        foreach ($processFieldList[$key]['data'][$tmpKey]['data'] as $tmpKey2 => $tmpData2) {
                                            if ($tmpData2['title'] == $tmpArr[1]) {
                                                $processFieldList[$key]['selectChild'] = $processFieldList[$key]['data'][$tmpKey]['data'];
                                                $processFieldList[$key]['selectChild'][$tmpKey2]['isSelect'] = 1;
                                            }
                                        }
                                    }
                                }
                            }


                        } elseif ($row['field_type'] == 13) {//多个勾选
                            //获取之前的工单id的配送信息
                            $oldRow = $curOrderProcessRow[$row['field_name']];
                            $tmpArr = explode(',', $oldRow);
                            foreach ($processFieldList[$key]['data'] as $tmpKey => $tmpValue) {
                                if (in_array($tmpValue['title'], $tmpArr)) {
                                    $processFieldList[$key]['data'][$tmpKey]['isCheck'] = 1;
                                }
                            }
                        } else {
                            $processFieldList[$key]['default_value'] = $curOrderProcessRow[$row['field_name']];
                        }
                    }
                }
            }

            $curProcess = [
                'process_id' => $curProcessId,
                'process_title' => $processTitle,
                'process_field_list' => $processFieldList,
            ];
            $processList[] = $curProcess;
            //下一个工序
            $curProcessId = $orderTypeObj->getNextOneProcessId($orderPreRow['order_type'], $curProcessId);//获取下一个工序
        }

        return $this->view('create-order-detail', compact('orderBaseFieldList', 'processList', 'orderPreId'));
    }

    //添加配送信息
    public function createOrderDistribution(Request $request)
    {
        //获取传入数据
        $input = $request->all();
        $orderPreId = $input['order_pre_id'];

        //如果已有信息，展示原数据
        $orderDistributionPreCollection = OrderDistributionPre::where(['order_id' => $orderPreId])->get();
        if ($orderDistributionPreCollection->count()) {
            $orderDistributionPreRow = $orderDistributionPreCollection->toArray();
            $orderDistributionPreRow = $orderDistributionPreRow[0];
        } else {
            $orderDistributionPreRow = [];
        }
        $provinceList = Province::where([])->get()->toArray();
        //市区默认为空，预留之后的默认选择功能
        if (isset($orderDistributionPreRow['province_id'])) {
            $cityList = City::where(['fatherID' => $orderDistributionPreRow['province_id']])->get()->toArray();
        } else {
            $cityList = [];
        }

        if (isset($orderDistributionPreRow['city_id'])) {
            $areaList = Area::where(['fatherID' => $orderDistributionPreRow['city_id']])->get()->toArray();
        } else {
            $areaList = [];
        }

        return $this->view('create-order-distribution', compact('orderPreId', 'provinceList', 'cityList', 'areaList', 'orderDistributionPreRow'));
    }

    //省市区联动，获取市区信息
    public function getCityAreaList(Request $request)
    {
        //获取传入数据
        $input = $request->all();
        $type = $input['type'];
        if ($type == 'getCityList') {
            $provinceID = $input['provinceID'];
            $cityList = City::where(['fatherID' => $provinceID])->get()->toArray();
            return $cityList;
        } elseif ($type == 'getAreaList') {
            $cityID = $input['cityID'];
            $AreaList = Area::where(['fatherID' => $cityID])->get()->toArray();
            return $AreaList;
        } else {
            return false;
        }
    }

    //添加配送信息-提交信息
    public function createOrderDistributionSubmit(Request $request)
    {
        //获取传入数据
        $input = $request->all();

        //用户id&企业id
        $userId = request()->header('userid', 0);
        $adminUser = request()->user('admin')->toArray();
        $userId = $adminUser['id'];
        $companyId = $adminUser['company_id'];

        //数据入库
        //创建预生成工单&返回预生成工单id
        $orderDistributionPreRow = OrderDistributionPre::firstOrNew(['order_id' => $input['order_pre_id']]);

        //获取省市区id对应的名称
        $input['province_name'] = Province::where(['provinceID' => $input['province_id']])->first()->province;
        $input['city_name'] = City::where(['cityID' => $input['city_id']])->first()->city;
        $input['area_name'] = Area::where(['areaID' => $input['area_id']])->first()->area;

        $insertRow = [
            'order_id' => $input['order_pre_id'],//关联的工单id
            'consignee' => $input['consignee'],//'收货人'
            'phone' => $input['phone'],//'电话'
            'province_id' => $input['province_id'],//'省-id'
            'province_name' => $input['province_name'],//'省-名称'
            'city_id' => $input['city_id'],//'市-id'
            'city_name' => $input['city_name'],//'市-名称'
            'area_id' => $input['area_id'],//'区-id'
            'area_name' => $input['area_name'],//'区-名称'
            'address' => $input['address'],//'详细地址'
            'company_id' => $companyId,//'详细地址'
        ];

        $orderDistributionPreRow->fill($insertRow);
        $orderDistributionPreRow->save();
        $orderDistributionPreRow->id;

        return [
            'code' => 0,
            'message' => '操作成功'
        ];
    }

    //添加材料
    public function pickMaterial(Request $request)
    {
        //获取传入数据
        $input = $request->all();

        $processId = $input['process_id'];
        $fieldName = $input['field_name'];
        return $this->view('create-order-pick-material', compact('processId', 'fieldName'));
    }

    //工单创建提交
    public function createOrderSubmit(Request $request)
    {
        //获取传入数据
        $input = $request->all();

        //用户id&企业id
        $adminUser = request()->user('admin')->toArray();
        $userId = $adminUser['id'];
        $companyId = $adminUser['company_id'];

        /****工单创建基础信息入库****/
        //数据入库
        //创建预生成工单&返回预生成工单id
        $orderPreId = $input['order_pre_id'];
        $orderPreRow = OrderPre::firstOrNew(['id' => $orderPreId]);

        //获取基础信息参数列表
        //用户id&企业id
        $userId = request()->header('userid', 0);
        $adminUser = request()->user('admin')->toArray();
        $userId = $adminUser['id'];
        $companyId = $adminUser['company_id'];
        $orderEngine = new OrderEngine();
        $orderBaseFieldList = $orderEngine->orderBaseFieldList($companyId);

        $updateOrderPreRow = [];
        foreach ($orderBaseFieldList as $orderBaseFieldRow) {
            if (in_array($orderBaseFieldRow['field_type'], array(8))) {//该字段类型不入库
                continue;
            }
            if ($orderBaseFieldRow['field_type'] == 5) {//文本+选择类型的字段特殊处理
                if (isset($input['base_' . $orderBaseFieldRow['field_name']]) && is_array($input['base_' . $orderBaseFieldRow['field_name']])) {
                    $updateOrderPreRow[$orderBaseFieldRow['field_name']] = implode(",", $input['base_' . $orderBaseFieldRow['field_name']]);
                }
            } elseif ($orderBaseFieldRow['field_type'] == 6) {//开关类型字段特出处理
                if (isset($input['base_' . $orderBaseFieldRow['field_name']])) {
                    $updateOrderPreRow[$orderBaseFieldRow['field_name']] = $input['base_' . $orderBaseFieldRow['field_name']];
                }
            } elseif ($orderBaseFieldRow['field_type'] == 9) {//图片上传处理
                if (isset($input['base_' . $orderBaseFieldRow['field_name']])) {
                    if (is_array($input['base_' . $orderBaseFieldRow['field_name']])) {
                        $updateOrderPreRow[$orderBaseFieldRow['field_name']] = implode(',', $input['base_' . $orderBaseFieldRow['field_name']]);
                    }
                }
            } else {
                $updateOrderPreRow[$orderBaseFieldRow['field_name']] = $input['base_' . $orderBaseFieldRow['field_name']];
            }
        }


        if (isset($input['base_product_num'])) {
            $tmpArr = $input['base_product_num'];
            if (!is_numeric($tmpArr[0])) {
                return [
                    'code' => 1,
                    'message' => '产品数量请填写数量'
                ];
            }
        }

        $updateOrderPreRow['is_trash'] = 0;
        $orderPreRow->fill($updateOrderPreRow);
        $orderPreRow->save();


        /****工单创建工单信息入库****/
        //第一个工序
        $orderPreRow = $orderPreRow->toArray();
        $orderTypeObj = new OrderTypeEngine();
        $firstProcessId = $orderTypeObj->getFirstOneProcessId($orderPreRow['order_type']);
        $curProcessId = $firstProcessId;

        //如果是第一道工序，添加字段需完成数量--功能调整,第一个工序需完成数量取产品的数量
        $orderInfo = OrderPre::where(['id' => $orderPreId])->first();
        $processFirstList = OrderTypeEngine::getFirstProcessBag($orderInfo['order_type'])->toArray();

        while ($curProcessId) {
            //通过工序id，获取对应的字段
            $processFieldList = Process::getFieldListByProcessId($curProcessId);

            //处理信息
            //验证字段是必填还是选填 by zzy
            $process_field_list = Process::getFieldListByProcessId($curProcessId);
            $process_field_rules = [];
            $process_field_message = [];
            foreach ($process_field_list as $key => $val) {
                $is_must = $val['is_must'];
                if ($is_must == 1) {
                    $rules_name = 'required';
                    $field_name = $val['field_name'];
                    $field_title = $val['title'];
                    $field_msg = $field_name . '.' . $rules_name;
                    $process_field_rules[$field_name] = $rules_name;
                    $process_field_message[$field_msg] = $field_title . '必填';

                    $input_value = $input[$curProcessId . '_' . $field_name];
                    if (empty($input_value)) {
                        return [
                            'code' => 1,
                            'message' => $field_title . '必填'
                        ];
                    }
                }
            }

            //获取工序所包含的字段
            $processFieldList = Process::getFieldListByProcessId($curProcessId);

            //数据入库
            //存入印刷工序提交的数据
            $orderProcessPreRow = OrderProcessPre::firstOrNew(['order_id' => $orderPreId, 'process_type' => $curProcessId]);

            $insertRow = [
                'order_id' => $orderPreId,//关联的工单id
                'process_type' => $curProcessId,//工序类型
                'status' => 1,//工单工序状态 1-待接单 2-确认开工（未领取材料） 3-已领取材料 4-确认完工
                'uid' => $userId,//用户Id
                'company_id' => $companyId,//企业Id
            ];

            //如果是第一道工序，添加字段需完成数量--功能调整,第一个工序需完成数量取产品的数量
            if (in_array($curProcessId, $processFirstList)) {
                //获取工单基础信息设置的数量
                $tmpArr = explode(',', $orderInfo['product_num']);
                $productNum = $tmpArr[0];
                if (!$productNum || !is_numeric($productNum)) {
                    $productNum = 0;
                }
                $insertRow['completed_number'] = $productNum;
            }

            foreach ($processFieldList as $processField) {
                if (in_array($processField['field_type'], array(8))) {//'选择地址',该字段类型不入库
                    continue;
                }
                if ($processField['field_type'] == 5 || $processField['field_type'] == 13) {//文本+选择类型|| 多个勾 选字段特殊处理
                    if (isset($input[$curProcessId . '_' . $processField['field_name']]) && is_array($input[$curProcessId . '_' . $processField['field_name']])) {
                        $insertRow[$processField['field_name']] = implode(",", $input[$curProcessId . '_' . $processField['field_name']]);
                    }
                } elseif ($processField['field_type'] == 6) {//开关类型字段特出处理
                    if (isset($input[$curProcessId . '_' . $processField['field_name']])) {
                        $insertRow[$processField['field_name']] = $input[$curProcessId . '_' . $processField['field_name']];
                    }
                } elseif ($processField['field_type'] == 11) {//双联动字段特出处理
                    //双联动第一个值需要由id转换为title
                    $dataValueList = [];
                    foreach ($processField['data'] as $data) {
                        $dataValueList[$data['id']] = $data['title'];
                    }
                    if (isset($input[$curProcessId . '_' . $processField['field_name']][0]) && $input[$curProcessId . '_' . $processField['field_name']][0]) {
                        $insertRow[$processField['field_name']] = $dataValueList[$input[$curProcessId . '_' . $processField['field_name']][0]] . ',';
                    }
                    if (isset($input[$curProcessId . '_' . $processField['field_name']][1]) && $input[$curProcessId . '_' . $processField['field_name']][1]) {
                        //第二个值直接使用，不需要转换值
                        $insertRow[$processField['field_name']] .= $input[$curProcessId . '_' . $processField['field_name']][1];
                    }

                } else {
                    if (isset($processField['field_name']) && !empty($processField['field_name'])) {
                        $insertRow[$processField['field_name']] = $input[$curProcessId . '_' . $processField['field_name']];
                    }
                }
            }


            $orderProcessPreRow->fill($insertRow);
            $orderProcessPreRow->save();

            //下一个工序
            $curProcessId = $orderTypeObj->getNextOneProcessId($orderPreRow['order_type'], $curProcessId);//获取下一个工序
        }

        //生成正式数据
        $orderId = $this->orderCreate($orderPreId);

        return [
            'code' => 0,
            'message' => '创建成功'
        ];
    }


    /**
     * Description:正式生成工单数据
     * User: zhuyujun
     * @param $orderPreId
     */
    public function orderCreate($orderPreId)
    {
        $orderId = '';
        //基础信息
        //取出预生成数据
        $orderPre = OrderPre::where('id', $orderPreId);
        $orderPreRow = $orderPre->first()->toArray();

        //获取登陆用户信息
        $adminUser = request()->user('admin')->toArray();
        $userId = $adminUser['id'];
        $companyId = $adminUser['company_id'];


        //存入正式数据
        $order = OrderEloquent::firstOrNew(['id' => '']);
        unset($orderPreRow['id']);//不使用预生成表的id
        unset($orderPreRow['is_trash']);//删除草稿箱表独有的字段
        $orderPreRow['order_pre_id'] = $orderPreId;//存入对应的草稿箱工单id
        $order->fill($orderPreRow);
        $order->save();
        $orderId = $order->id;
        //删除预生成数据
        //基础信息
        $orderPre->delete();

        //配送信息
        //取出预生成数据
        $orderDistributionPre = OrderDistributionPre::where('order_id', $orderPreId);
        $orderDistributionPreRow = $orderDistributionPre->first();
        if ($orderDistributionPreRow) {
            $orderDistributionPreRow = $orderDistributionPreRow->toArray();
            //存入正式数据
            $orderDistribution = OrderDistribution::firstOrNew(['id' => '']);
            $orderDistributionPreRow['order_id'] = $orderId;
            $orderDistribution->fill($orderDistributionPreRow);
            $orderDistribution->save();
            $orderDistribution->id;
            //删除预生成数据
            $orderDistributionPre->delete();
        }


        //工序信息处理
        $orderProcessPreList = OrderProcessPre::where('order_id', $orderPreId)->get();

        //获取第一道步骤涉及的工序
        $processFirstList = OrderTypeEngine::getFirstProcessBag($orderPreRow['order_type'])->toArray();

        foreach ($orderProcessPreList as $orderProcessPreObj) {
            $orderProcessPreRow = $orderProcessPreObj->toArray();
            //存入正式数据
            $orderProcess = OrderProcessEloquent::firstOrNew(['id' => '']);
            $orderProcessPreRow['order_id'] = $orderId;
            $orderProcess->fill($orderProcessPreRow);
            $orderProcess->save();


            //添加印刷材料
            //获取字段信息
            $processFieldList = Process::getFieldListByProcessId($orderProcessPreRow['process_type']);
            foreach ($processFieldList as $processFieldRow) {
                $tmpFiledType = ProcessField::getFieldTypeByFieldName($processFieldRow['field_name'], $companyId);
                if ($tmpFiledType == 4) {//材料选择暂时不显示在
                    $materialList = explode(',', $orderProcessPreRow[$processFieldRow['field_name']]);
                    foreach ($materialList as $materialsId) {
                        //过滤空格
                        if (!trim($materialsId)) {
                            continue;
                        }
                        $orderMaterialObj = OrderMaterial::firstOrNew(['order_id' => $orderProcessPreRow['order_id'], 'process_type' => $orderProcessPreRow['process_type'], 'material_id' => $materialsId]);
                        $orderMaterialRow = [
                            'order_id' => $orderProcessPreRow['order_id'],
                            'process_type' => $orderProcessPreRow['process_type'],
                            'material_id' => $materialsId,
                            'company_id' => $companyId,
                        ];
                        $orderMaterialObj->fill($orderMaterialRow);
                        $orderMaterialObj->save();
                    }
                }
            }

            //删除预生成数据
            $orderProcessPreObj->delete();
        }


        //增加订单类型使用次数
        $orderTypeId = $orderPreRow['order_type'];
        OrderTypeEngine::incrementUseNum($orderTypeId);

        return $orderId;
    }

    //下单人工单分配类型
    public function getOrderManageAssignmentType()
    {
        $orderId = request('order_id', '');

        //获取登陆用户信息
        $adminUser = request()->user('admin')->toArray();
        $userId = $adminUser['id'];
        $companyId = $adminUser['company_id'];

        $orderEngine = new OrderEngine();
        return $orderEngine->getOrderManageAssignmentType($orderId, $userId, $companyId);
    }


    //工单派发页面基础页面
    public function orderConfirmAssignmentBaseInfo()
    {
        $orderId = request('order_id');//工单id
        $orderRow = OrderEloquent::where(['id' => $orderId])->first()->toArray();
        $orderType = $orderRow['order_type'];

        //获取需分派的工序管理员列表
        $orderEngine = new OrderEngine();
        $result = $orderEngine->distributionAction($orderType);

        return $this->view('order-Assignment', compact('result', 'orderId'));
    }


    //工单派发
    public function orderConfirmAssignment()
    {
        $orderId = request('order_id');//工单id
        //获取登陆用户信息
        $adminUser = request()->user('admin')->toArray();
        $userId = $adminUser['id'];
        $companyId = $adminUser['company_id'];

        //获取分配用户列表
        $orderRow = OrderEloquent::where(['id' => $orderId])->first()->toArray();
        $orderType = $orderRow['order_type'];
        $result = OrderTypeEngine::getAllStepsWithDistribution($orderType);

        $result->transform(function ($item) {
//            $item->distributionUser = Privilege::getWithDepartmentUser( $item->distribution );
            $distributionUser = DepartmentUser::getWithPrivilege($item->distribution);
            $process_id = $item->process_id;
            $distributionUser->transform(function ($item) use ($process_id) {
                $item->process_id = $process_id;
                $item->ygtUser->avatar = $item->ygtUser->avatar ? Func::getImgUrlById($item->ygtUser->avatar) : asset('upload/appicon/logo.png');
                return $item;
            });
            $item->distributionUser = $distributionUser->toArray();
            return $item;
        });

        $result = $result->filter(function ($item) {
            if ($item->distribution->toArray()) return true;
            return false;
        });

        $assignUserList = [];
        foreach ($result as $tmpRow) {
            $tmpProcessId = $tmpRow['process_id'];
            $tmpUid = request('processid_' . $tmpProcessId);
            if (!$tmpUid) {
                return
                    [
                        'code' => 1,
                        'message' => '未选择分配对象'
                    ];
            }
            $assignUserList[] = [
                'process_id' => $tmpProcessId,
                'uid' => $tmpUid
            ];

        }

        $orderEngine = new OrderEngine();
        return $orderEngine->orderConfirmAssignment($orderId, $assignUserList, $userId);


    }

    //工单再来一单
    public function copyOrder()
    {
        $oldOrderId = request('copy_order_id');//旧工单id

        if (!$oldOrderId) {
            return [
                'code' => 1,
                'message' => '参数错误'
            ];
        }

        $curOrderRow = OrderEloquent::where(['id' => $oldOrderId])->first()->toArray();

        //创建预生成工单&返回预生成工单id
        //生成工单号
        $orderTitle = 'D' . time() . rand(0, 9) . rand(0, 9) . rand(0, 9) . rand(0, 9) . rand(0, 9) . rand(0, 9);

        //用户id&企业id
        $adminUser = request()->user('admin')->toArray();
        $userId = $adminUser['id'];
        $companyId = $adminUser['company_id'];

        $insertRow = [
            'order_type' => $curOrderRow['order_type'],
            'order_title' => $orderTitle,
            'uid' => $userId,
            'company_id' => $companyId,
            'status' => 4,//工单状态 4-待派发
        ];

        $orderPreRow = OrderPre::firstOrNew(['id' => '']);
        $orderPreRow->fill($insertRow);
        $orderPreRow->save();
        $orderPreId = $orderPreRow->id;

        //复制对应的工单派送地址
        $curOrderDistributionCollection = OrderDistribution::where(['order_id' => $curOrderRow['id']])->get();
        if ($curOrderDistributionCollection->count()) {//存在派送信息
            $curOrderDistributionCollectionRow = $curOrderDistributionCollection->first()->toArray();
            $curOrderDistributionCollectionRow['order_id'] = $orderPreId;
            $orderDistributionPreRow = OrderDistributionPre::firstOrNew(['id' => '']);
            $orderDistributionPreRow->fill($curOrderDistributionCollectionRow);
            $orderDistributionPreRow->save();
            $orderDistributionPreId = $orderDistributionPreRow->id;
        }
        header("location:/admin/order/createOrderDetail?order_pre_id={$orderPreId}&copy_order_id={$oldOrderId}");
        die();
    }

    //工单报表类型列表
    public function orderReportType()
    {
        return $this->view('order-report-type-list');
    }

    //工单报表类型列表-数据获取
    public function getOrderReportTypeListData()
    {
        $callBack = request('callback');

        //用户id&企业id
        $adminUser = request()->user('admin')->toArray();
        $userId = $adminUser['id'];
        $companyId = $adminUser['company_id'];

        //获取工单报表类型列表
        $orderReportTypeList = OrderReportTypeEloquent::where(['company_id' => $companyId])->get();

        echo $callBack . '(' . $orderReportTypeList->toJson() . ')';
        die();
    }

    //工单报表类型编辑
    public function eidtOrderReportType()
    {
        $orderReportTypeId = request('order_report_type_id');

        $orderReprotTypeRow = OrderReportTypeEloquent::where(['id' => $orderReportTypeId])->first();
        return $this->view('order-report-type-edit', compact('orderReprotTypeRow'));

    }

    //工单报表类型编辑-数据提交
    public function eidtOrderReportTypeSubmit()
    {
        //用户id&企业id
        $adminUser = request()->user('admin')->toArray();
        $userId = $adminUser['id'];
        $companyId = $adminUser['company_id'];

        $orderReportTypeId = request('order_report_type_id');

        $title = request('title');
        $orderReportTypeRow = [];
        $orderReportTypeRow['title'] = $title;
        $orderReportTypeRow['company_id'] = $companyId;

        $orderReprotType = OrderReportTypeEloquent::firstOrNew(['id' => $orderReportTypeId]);
        $orderReprotType->fill($orderReportTypeRow);
        $orderReprotType->save();

        return [
            'code' => 0,
            'msg' => '操作成功'
        ];

    }

    //工单报表类型删除
    public function delOrderReportType()
    {
        $orderReportTypeId = request('id');

        OrderReportTypeEloquent::where(['id' => $orderReportTypeId])->delete();

        return [
            'code' => 0,
            'msg' => '操作成功'
        ];
    }

    //工单类型设置报表内容
    public function chooseOrderReportCell()
    {
        $orderReportTypeId = request('order_report_type_id');

        //获取工单报表信息
        $orderReportTypeRow = OrderReportTypeEloquent::where(['id' => $orderReportTypeId])->first();
        $content = $orderReportTypeRow['content'];
        $contentList = explode(',', $content);

        //获取工单报表内容列表
        $orderReportCellList = OrderReportCellEloquent::all();

        $orderReportCellList->transform(function ($item, $key) use ($contentList) {
            $item['isCheck'] = in_array($item['id'], $contentList) ? 1 : 0;
            return $item;
        });

        return $this->view('choose-order-report-cell', compact('orderReportCellList', 'orderReportTypeId'));
    }

    //工单类型设置报表内容-数据提交
    public function chooseOrderReportCellSubmit()
    {
        $orderReportTypeId = request('order_report_type_id');
        $orderReportCells = request('order_report_cells');
        $orderReportCellsStr = implode(',', $orderReportCells);

        //获取工单报表信息
        $orderReportType = OrderReportTypeEloquent::where(['id' => $orderReportTypeId])->first();

        $orderReportTypeRow = [];
        $orderReportTypeRow['content'] = $orderReportCellsStr;

        $orderReportType->fill($orderReportTypeRow);
        $orderReportType->save();

        return [
            'code' => 0,
            'msg' => '操作成功'
        ];
    }

    //工单类型设置报表发送用户
    public function chooseSendUser()
    {
        //用户id&企业id
        $adminUser = request()->user('admin')->toArray();
        $userId = $adminUser['id'];
        $companyId = $adminUser['company_id'];

        $orderReportTypeId = request('order_report_type_id');

        //获取工单报表信息
        $orderReportTypeRow = OrderReportTypeEloquent::where(['id' => $orderReportTypeId])->first();
        $sendUser = $orderReportTypeRow['send_user'];
        $sendUserList = explode(',', $sendUser);

        //获取工单报表内容列表
        $orderReportCellList = OrderReportCellEloquent::all();

        $privilegList = collect(Privilege::getList(['company_id' => $companyId], 'id,title')->toArray());

        $privilegList->transform(function ($item, $key) use ($sendUserList) {
            $item['isCheck'] = in_array($item['id'], $sendUserList) ? 1 : 0;
            return $item;
        });

        return $this->view('choose-send-user', compact('privilegList', 'orderReportTypeId'));
    }

    //工单类型设置报表发送用户-数据提交
    public function chooseSendUserSubmit()
    {
        $orderReportTypeId = request('order_report_type_id');
        $sendUsers = request('send_users');
        $sendUsersStr = implode(',', $sendUsers);

        //获取工单报表信息
        $orderReportType = OrderReportTypeEloquent::where(['id' => $orderReportTypeId])->first();

        $orderReportTypeRow = [];
        $orderReportTypeRow['send_user'] = $sendUsersStr;

        $orderReportType->fill($orderReportTypeRow);
        $orderReportType->save();

        return [
            'code' => 0,
            'msg' => '操作成功'
        ];
    }

    public function getOrderProcessCourseList(Request $request){
        $input = $request->all();
        $orderProcessId = isset($input['order_process_id']) ? $input['order_process_id'] : '';

        return $this->view('order-process-course-list', compact('orderProcessId'));
    }

    public function getOrderProcessCourseListData(Request $request)
    {
        //后台获取用户对应的企业id
        $adminUser = request()->user('admin')->toArray();
        $companyId = $adminUser['company_id'];

        $input = $request->all();
        $callBack = $input['callback'];
        $orderProcessId = isset($input['order_process_id']) ? $input['order_process_id'] : '';
        $searchStatus = isset($input['search_status']) ? $input['search_status'] : '';

        $order = new Order();
        $where = [];
        $where = ['company_id' => $companyId];
        if ($orderProcessId) {
            $where['order_process_id'] = $orderProcessId;
        }

        if($searchStatus){
            $where['status'] =$searchStatus;
        }

        $orderProcessCourseList = $order->getOrderProcessCourseList($where);

        //数据调整
        foreach ($orderProcessCourseList as $tmpOrderProcessCourseRow) {
//            $tmpOrderProcessCourseRow->process_title = $order->getProcessTitle($tmpOrderProcessCourseRow->process_type);//工序名称
            $tmpOrderProcessCourseRow->process_title = '';//工序名称

            //开工时间
            if ($tmpOrderProcessCourseRow->start_time) {
                $tmpOrderProcessCourseRow->start_time = date('Ymd H:i:s', $tmpOrderProcessCourseRow->start_time);
            } else {
                $tmpOrderProcessCourseRow->start_time = '';
            }


            //预计完成时间
            if ($tmpOrderProcessCourseRow->status != 1) {
                if (!(int)$tmpOrderProcessCourseRow->estimated_time_hour) $tmpOrderProcessCourseRow->estimated_time_hour = 0;
                if (!(int)$tmpOrderProcessCourseRow->estimated_time_minute) $tmpOrderProcessCourseRow->estimated_time_minute = 0;
                $tmpOrderProcessCourseRow->estimated_time = date('Ymd H:i:s', (strtotime($tmpOrderProcessCourseRow->start_time) + $tmpOrderProcessCourseRow->estimated_time_hour * 3600 + $tmpOrderProcessCourseRow->estimated_time_minute * 60));
                $tmpOrderProcessCourseRow->estimated_time .= "({$tmpOrderProcessCourseRow->estimated_time_hour}h{$tmpOrderProcessCourseRow->estimated_time_minute}m)";
            } else {
                $tmpOrderProcessCourseRow->estimated_time = '';
            }

            //确认完工时间
            if ($tmpOrderProcessCourseRow->status == 4) {
                $tmpOrderProcessCourseRow->completed_time = $tmpOrderProcessCourseRow->updated_at->formatLocalized('%Y%m%d %H:%M:%S');//开工时间
            } else {
                $tmpOrderProcessCourseRow->completed_time = '';
            }

            $tmpOrderProcessCourseRow->process_order_status = $order->getOrderProcessCourseStatusById($tmpOrderProcessCourseRow->status);//工单状态
            $tmpOrderProcessCourseRow->accept_user = (string)$order->getUserNameById($tmpOrderProcessCourseRow->uid);//接单人
        }

        echo $callBack . '(' . $orderProcessCourseList->toJson() . ')';
        die();
    }


    //工单基础页面字段
    public function orderBaseFieldList()
    {
        return [
            [
                'field_name' => 'customer_name',
                'title' => '客户',
                'field_type' => 1,
                'placeholder' => '请填写',
                'relation_id' => '',
                'is_must' => 1,
                'field_unit' => [],
                'data' => [],
            ],
            [
                'field_name' => 'product_name',
                'title' => '品名',
                'field_type' => 1,
                'placeholder' => '请填写',
                'relation_id' => '',
                'is_must' => 1,
                'field_unit' => [],
                'data' => [],
            ],
            [
                'field_name' => 'contract_number',
                'title' => '合同编号',
                'field_type' => 1,
                'placeholder' => '请填写',
                'relation_id' => '',
                'is_must' => 1,
                'field_unit' => [],
                'data' => [],
            ],
            [
                'field_name' => 'enterprise_icon',
                'title' => '企业图标',
                'field_type' => 9,
                'placeholder' => '请上传',
                'new_page_title' => '上传企业图标',
                'relation_id' => '',
                'is_must' => 1,
                'field_unit' => [],
                'data' => [],
            ],
            [
                'field_name' => 'finished_date',
                'title' => '交货日期',
                'field_type' => 7,
                'placeholder' => '请填写',
                'relation_id' => '',
                'is_must' => 1,
                'field_unit' => [],
                'data' => [],
            ],
            [
                'field_name' => 'add_distribution_address',
                'title' => '添加配送地址',
                'field_type' => 8,
                'placeholder' => '请填写',
                'relation_id' => '',
                'is_must' => 1,
                'field_unit' => [],
                'data' => [],
            ],
            [
                'field_name' => 'product_num',
                'title' => '产品数量',
                'field_type' => 5,
                'placeholder' => '请填写',
                'relation_id' => '',
                'is_must' => 1,
                'field_unit' => [
                    [
                        'id' => 1,
                        'title' => 'kg'
                    ],
                    [
                        'id' => 2,
                        'title' => 'cm'
                    ],
                    [
                        'id' => 3,
                        'title' => 'L'
                    ],
                    [
                        'id' => 4,
                        'title' => 't'
                    ],
                    [
                        'id' => 5,
                        'title' => '㎡'
                    ]
                ],
                'data' => [],
            ],
            [
                'field_name' => 'chip_specification_length',
                'title' => '片料规格-长',
                'field_type' => 1,
                'placeholder' => '请填写',
                'relation_id' => '',
                'is_must' => 1,
                'field_unit' => [],
                'data' => [],
            ],
            [
                'field_name' => 'chip_specification_width',
                'title' => '片料规格-宽',
                'field_type' => 1,
                'placeholder' => '请填写',
                'relation_id' => '',
                'is_must' => 1,
                'field_unit' => [],
                'data' => [],
            ],
            [
                'field_name' => 'grammage',
                'title' => '克数',
                'field_type' => 1,
                'placeholder' => '请填写',
                'relation_id' => '',
                'is_must' => 1,
                'field_unit' => [],
                'data' => [],
            ],
            [
                'field_name' => 'weaving_type',
                'title' => '织布类型',
                'field_type' => 1,
                'placeholder' => '请填写',
                'relation_id' => '',
                'is_must' => 1,
                'field_unit' => [],
                'data' => [],
            ],
            [
                'field_name' => 'plate_numbering',
                'title' => '印版编号',
                'field_type' => 1,
                'placeholder' => '请填写',
                'relation_id' => '',
                'is_must' => 1,
                'field_unit' => [],
                'data' => [],
            ],
            [
                'field_name' => 'plate_position',
                'title' => '印版位置',
                'field_type' => 1,
                'placeholder' => '请填写',
                'relation_id' => '',
                'is_must' => 1,
                'field_unit' => [],
                'data' => [],
            ],
            [
                'field_name' => 'public_plate',
                'title' => '公用印版',
                'field_type' => 1,
                'placeholder' => '请填写',
                'relation_id' => '',
                'is_must' => 1,
                'field_unit' => [],
                'data' => [],
            ],
            [
                'field_name' => 'plate_modification',
                'title' => '印版修改',
                'field_type' => 1,
                'placeholder' => '请填写',
                'relation_id' => '',
                'is_must' => 1,
                'field_unit' => [],
                'data' => [],
            ],
            [
                'field_name' => 'finished_weight',
                'title' => '成品重量',
                'field_type' => 1,
                'placeholder' => '请填写',
                'relation_id' => '',
                'is_must' => 1,
                'field_unit' => [],
                'data' => [],
            ],
            [
                'field_name' => 'is_priority',
                'title' => '优先处理',
                'field_type' => 6,
                'placeholder' => '请填写',
                'relation_id' => '',
                'is_must' => 1,
                'field_unit' => [],
                'data' => [],
            ],
            [
                'field_name' => 'production_case_diagram',
                'title' => '生产实例图',
                'field_type' => 9,
                'placeholder' => '请上传',
                'new_page_title' => '上传企业图标',
                'relation_id' => '',
                'is_must' => 1,
                'field_unit' => [],
                'data' => [],
            ]
        ];
    }

    public function getUnitIdByTitle($unitTitle, $unitList)
    {
        foreach ($unitList as $unitRow) {
            if ($unitRow['title'] == $unitTitle) {
                return $unitRow['id'];
            }
        }
        return false;
    }

    public function getSidByTitle($dataTitle, $dataList)
    {
        foreach ($dataList as $dataRow) {
            if ($dataRow['title'] == $dataTitle) {
                return $dataRow['id'];
            }
        }
        return false;
    }


    public function test(){
        return $this->view('test');
    }


}

if (!function_exists('request')) {
    /**
     * Get an instance of the current request or an input item from the request.
     *
     * @param  array|string $key
     * @param  mixed $default
     * @return \Illuminate\Http\Request|string|array
     */
    function request($key = null, $default = null)
    {
        if (is_null($key)) {
            return app('request');
        }

        if (is_array($key)) {
            return app('request')->only($key);
        }

        return data_get(app('request')->all(), $key, $default);
    }
}